Imports GeoFramework.IO
Imports GeoFramework.Gps.Emulators
Imports GeoFramework.Gps.Nmea

Public Class MainForm

    Public Shared MainExampleForm As MainForm

    '' <summary>
    '' The main entry point for the application.
    '' </summary>
    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        MainExampleForm = New MainForm

        ' NOTE: Trial license keys are required to make this sample work on your
        ' computer.  License keys can be activated instantly by going to this
        ' address: http://www.geoframeworks.com/FreeTrial.aspx.  License keys can
        ' then be added in the "AssemblyInfo" file in this project.

        ' Start searching for GPS devices in the background
        Devices.Detect()

        ' Hook into device detection events so you can see what's going on
        AddHandler Devices.DeviceDetectionAttempted, AddressOf Devices_DeviceDetectionAttempted
        AddHandler Devices.DeviceDetectionCompleted, AddressOf Devices_DeviceDetectionCompleted
        AddHandler Devices.DeviceDetected, AddressOf Devices_DeviceDetected

        ' Start the main form as usual
        Application.Run(MainExampleForm)
    End Sub

    Private Shared Sub Devices_DeviceDetectionCompleted(ByVal sender As Object, ByVal e As EventArgs)
        ' If no GPS device has been detected, offer to use an emulator
        If Not Devices.IsDeviceDetected Then
            Select Case MessageBox.Show("No GPS devices were found to be connected to the computer.  Do you want to continue the example using a virtual GPS device?", "Use Virtual GPS Device", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                Case DialogResult.Yes
                    ' Create and register a virtual GPS device
                    Devices.RegisterGpsDevice(New VirtualDevice(New DeLormeEarthmateUsbEmulator))
            End Select
        End If
    End Sub

    Private Shared Sub Devices_DeviceDetectionAttempted(ByVal sender As Object, ByVal e As DeviceEventArgs)
        ' Update the status bar
        MainExampleForm.toolStripStatusLabel1.Text = "Checking for GPS device on " + e.Device.ToString()
    End Sub

    Private Shared Sub Devices_DeviceDetected(ByVal sender As Object, ByVal e As DeviceEventArgs)
        ' Update the status bar
        MainExampleForm.toolStripStatusLabel1.Text = "A GPS device was found! " + e.Device.ToString()
    End Sub


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    Private Sub CloseOpenForms()
        ' Close any child forms
        Dim ExistingForm As Form
        For Each ExistingForm In MdiChildren
            ExistingForm.Close()
            ExistingForm.Dispose()
        Next
    End Sub

    Private Sub ShowForm(ByVal form As Form)
        ' Then open the new one
        form.MdiParent = Me
        form.WindowState = FormWindowState.Normal
        form.BackColor = Color.White
        form.MaximizeBox = False
        form.MinimizeBox = False
        form.Show()

        LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub ConnectingAndDisconnectingLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles ConnectingAndDisconnectingLink.LinkClicked
        CloseOpenForms()
        ShowForm(New ConnectingAndDisconnectingForm)
    End Sub

    Private Sub CurrentLocationLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CurrentLocationLink.LinkClicked
        CloseOpenForms()
        ShowForm(New LiveDataForm)
    End Sub

    Private Sub DetectingDevicesLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles DetectingDevicesLink.LinkClicked
        CloseOpenForms()
        ShowForm(New DetectingDevicesForm)
    End Sub

    Private Sub AsynchronousDeviceDetectionLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AsynchronousDeviceDetectionLink.LinkClicked
        CloseOpenForms()
        ShowForm(New AsynchronousDetectingDevicesForm)
    End Sub

    Private Sub MonitoringDeviceDetectionLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles MonitoringDeviceDetectionLink.LinkClicked
        CloseOpenForms()
        ShowForm(New MonitoringDetectingDevicesForm)
    End Sub

    Private Sub ParsingStringsLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles ParsingStringsLink.LinkClicked
        CloseOpenForms()
        ShowForm(New StringParsingForm)
    End Sub

    Private Sub CompassLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CompassLink.LinkClicked
        CloseOpenForms()
        ShowForm(New CompassForm)
    End Sub

    Private Sub ControlPerformanceTuningLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles ControlPerformanceTuningLink.LinkClicked
        CloseOpenForms()
        ShowForm(New ControlPerformanceTuningForm)
    End Sub

    Private Sub AltimeterLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linkLabel9.LinkClicked
        CloseOpenForms()
        ShowForm(New AltimeterForm)
    End Sub

    Private Sub SpeedometerLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles SpeedometerLink.LinkClicked
        CloseOpenForms()
        ShowForm(New SpeedometerForm)
    End Sub

    Private Sub SatelliteViewerLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles SatelliteViewerLink.LinkClicked
        CloseOpenForms()
        ShowForm(New SatelliteViewerWithSignalBarForm)
    End Sub

    Private Sub CustomDeviceEmulatorsLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CustomDeviceEmulatorsLink.LinkClicked
        CloseOpenForms()
        ShowForm(New CustomDeviceEmulatorForm)
    End Sub

    Private Sub WorkingWithEmulatorsLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles WorkingWithEmulatorsLink.LinkClicked
        CloseOpenForms()
        ShowForm(New WorkingWithEmulatorsForm)
    End Sub

    Private Sub CalculatingBearingLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CalculatingBearingLink.LinkClicked
        CloseOpenForms()
        ShowForm(New CalculateBearingForm)
    End Sub

    Private Sub CalculatingDistanceLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CalculatingDistanceLink.LinkClicked
        CloseOpenForms()
        ShowForm(New CalculateDistanceForm)
    End Sub

    Private Sub SynchronizingTheClockLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles SynchronizingTheClockLink.LinkClicked
        CloseOpenForms()
        ShowForm(New SynchronizingTheClockForm)
    End Sub

    Private Sub LoggingGpsDataLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LoggingGpsDataLink.LinkClicked
        CloseOpenForms()
        ShowForm(New LoggingGpsDataForm)
    End Sub

    Private Sub UnderstandingPrecisionLink_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles UnderstandingPrecisionLink.LinkClicked
        CloseOpenForms()
        ShowForm(New UnderstandingPrecisionForm)
    End Sub

End Class
